package com.flyxteam.commons.codec;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.commons.codec.binary.Base64;

import com.flyxteam.commons.base.consts.GlobalConsts;

/**
 * Base64Util
 * <ul>
 * <li>2017-01-01 | ChrisX | + create</li>
 * </ul>
 * @author ChrisX
 */
public class Base64Util {

    private Base64Util() {}
    
	public static String encodeByByte(byte[] bytes) {
		return new String(Base64.encodeBase64(bytes), Charset.forName(GlobalConsts.ENCODING));
	}
	
	public static String encodeByString(String source) {
	    String target = null;
        try {
            target = new String(Base64.encodeBase64(source.getBytes(GlobalConsts.ENCODING)));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return target;
    }
	
	public static byte[] decodeToByte(String source) {
        return Base64.decodeBase64(source.getBytes(Charset.forName(GlobalConsts.ENCODING)));
    }
    
    public static String decodeToString(String source) {
        String target = null;
        try {
            target = new String(Base64.decodeBase64(source.getBytes()), GlobalConsts.ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return target;
    }
}